var warehouseSelecturl = ecp_boss_server + "/openx/wms/virtual/listVirtualWarehouseByPage?code=json";
var warehouseParam = {};
function openWarehouseSelect(selectDatas,callback) {
    var html='<style>.select2-container--open {z-index: 10000000000}</style>'
    +'<div class="ibox float-e-margins">'
        +'<div class="ibox-content">'
        +'<form class="form-horizontal" id="warehouseSelectSearchForm" action="#">'
        +'<div class="form-group">'
        +'<div class="col-sm-12">'
        +'<div class="row">'
        +'<div class="col-md-1" style="width:auto"><label class="control-label">仓库编号</label></div>'
        +'<div class="col-md-3">'
        +'<input placeholder="仓库编号" name="warehouseCode" class="form-control" type="text">'
        +'</div>'
        +'<div class="col-md-1" style="width:auto"><label class="control-label">仓库名称</label></div>'
        +'<div class="col-md-3">'
        +'<input placeholder="仓库名称" name="warehouseName" class="form-control" type="text">'
        +'</div>'
        +'<div class="col-offset-4">'
        +'<button type="button" class="btn btn-success" id="query" onclick="selectSearch()">查询</button> &nbsp;&nbsp;'
        +'<button type="button" class="btn btn-success"onclick="javascript:$(\'#warehouseSelectSearchForm\')[0].reset()">重置</button>'
    +'  </div>'
    +'  </div>'
    +'  </div>'
    +'  </div>'
    +'   <div class="hr-line-dashed"></div>'
    +'  </form>'
    +'    <div class="row row-lg">'
    +'   <div class="col-sm-12">'
    +'    <table id="warehouseSelectTable" data-unique-id="id">'
    +'    <thead>'
    +'    <tr>'
    +'    <th data-field="state" data-checkbox="true"></th>'
/*    +'  <th data-align="center" data-formatter="generatorId">序号</th>'*/
    +'  <th data-align="center" data-field="name" data-sortable="false">仓库名称</th>'
    +'  <th data-align="center" data-field="warehouseCode">仓库编号</th>'
/*  +'  <th data-align="center" data-field="areaCode" data-formatter="selectProvinceFormatter">省</th>'*/
/*    +'  <th data-align="center" data-field="areaCode" data-formatter="selectCityFormatter">市</th>'*/
/*    +'  <th data-align="center" data-field="warehousePhone">联系电话</th>'*/
    +'  <th data-align="center" data-field="address">地址</th>'
    +'  <th data-align="center" data-field="longitude">经度</th>'
    +'  <th data-align="center" data-field="latitude">纬度</th>'
    +'  <th data-align="center" data-field="status" data-formatter="selectWarehouseStatusFormatter">状态 </th>'
    +' </tr>'
    +'  </thead>'
    +'  </table>'
    +'  </div>'
    +'  </div>'
    +'  </div>'
    +'  </div>';

    var selectWarehouseIndex = layer.open({
        type: 1,
        area: ['650px', '600px'],
        shadeClose: true, // 点击遮罩关闭
        maxmin: false,
        moveOut: true,
        title: '选择仓库信息',
        content: html,
        shift: 2,
        btn: ['确定', '取消', '清空'], //按钮
        success: function(layero, index){
            initTable();
        },
        yes: function (index, layero) {
            var selecedData = $('#warehouseSelectTable').bootstrapTable('getAllSelections');
            if(selecedData){
                if(selecedData.length===0){
                    layer.msg("请选择一个仓库");
                    return false;
                }else if(selecedData.length>1){
                    layer.msg("您只能选择一个仓库");
                    return false;
                }
                callback(selecedData);
                layer.close(index);
            }else{
                layer.msg("请选择一个仓库");
                return false;
            }

        },
        btn2: function (index, layero) {
            layer.close(index);
        },
        btn3: function (index, layero) {
            callback(null);
            layer.close(index);
        }
    });



}

function initTable(){
    var warehouseUrl = ecp_boss_server + "/openx/wms/virtual/listVirtualWarehouseByPage?code=json";
    var warehouseparams = {};
    warehouseparams.virtualWarehouse = {};
    //封装分页对象 默认从第一条开始  每页10条
    var request = {};
    //layer.load();// 显示加载提示
    // 定义表格的默认样式
    request.queryParams= function (data) {
        var page = {};
        page.offset =data.offset;
        page.pageSize =data.limit;
        warehouseparams.page = page;
        warehouseparams._openx_head=getOpenxHeader();
        return warehouseparams;
    }
    $("#warehouseSelectTable").bootstrapTable({
        url:warehouseUrl,
        queryParams:request.queryParams,
        pagination : true,
        align:"center",
        striped : false,
        search : false,
        showToggle : false,
        pageList:[5,10, 25, 50, 100],
        method:'post',
        showRefresh:false,
        silent:false,
        sidePagination:'server',
        showColumns : false,
        toolbar : "",
        iconSize : "outline",
        icons : {
            toggle : "glyphicon-list-alt",
            columns : "glyphicon-list",
            refresh:"glyphicon-refresh icon-refresh"
        },
        onPageChange:function (num,size) {

        },
        responseHandler:function (res) {
            return res;
        }
    });

    $("#warehouseSelectTable").on('click-row.bs.table', function(e, row, $element) {
        // 设置表格选择后的颜色
        $('.success').removeClass('success');
        $($element).addClass('success');
    });

    // 对应事件 onLoadSuccess
    $("#warehouseSelectTable").on('load-success.bs.table', function(e, row, $element) {
        layer.closeAll('loading');
    });

    //对应事件 onLoadError
    $("#warehouseSelectTable").on("load-error.bs.table",function(xhr,error){
        layer.closeAll('loading');
        if(error&&error.status&&error.status===500){
            var errorJSON=error.responseJSON||null;
            if(errorJSON&&errorJSON.code==='70009999'){
                layer.alert(errorJSON.message, {
                    // skin: 'layui-layer-molv' //样式类名
                    title:'登录信息',
                    closeBtn: 0
                }, function(){
                    location.href="/login.html";
                });

            }
        }else{
            layer.msg("数据加载失败,服务器出错");
        }
    });


}

function selectSearch() {
    warehouseParam.virtualWarehouse = {};
    //获取查询条件
    var warehouseCode=$('#warehouseSelectSearchForm input[name="warehouseCode"]').val().trim();
    var warehosueName=$('#warehouseSelectSearchForm input[name="warehouseName"]').val().trim();
    if(warehouseCode.length>0){
        warehouseParam.virtualWarehouse.warehouseCode=warehouseCode;
    }
    if (warehosueName.length>0){
        warehouseParam.virtualWarehouse.name=warehosueName;
    }

    //参数一 ： 传递请求后台的URL地址  参数二: 传递和Api接口对于的json对象数据， 以下刷新方法主要用于 当改变表单的查询条件时使用
    $("#warehouseSelectTable").bootstrapTable('refresh',{url:warehouseSelecturl,query:warehouseParam});
}

function selectWarehouseStatusFormatter(value, row) {
    if (value === 'CLOSE') {
        return '<font color="red">停业</font>'
    } else if (value === 'OPEN') {
        return '<font color="green">营业</font>'
    }
    return '';
}


function selectProvinceFormatter(value, row) {
    var area=queryAreaDetailByAreaCode(row.areaCode);
    if(area&&area.province){
        return area.province;
    }
    return '';
}
function selectCityFormatter(value, row) {
    var area=queryAreaDetailByAreaCode(row.areaCode);
    if(area&&area.city){
        return area.city;
    }
    return '';
}


